const fs = require('fs')
const path = require('path')
const basePath = path.resolve(__dirname, '../src')

let dirStr = process.argv[2];
let dirName = "";
let ind = 0;

if(dirStr.indexOf("/") !== -1) {
  ind = dirStr.lastIndexOf("/");
  dirName = dirStr.substring(ind + 1);
} else {
  dirName = dirStr;
}
moduleName = dirName.replace(dirName[0],dirName[0].toUpperCase());

function mkdirSync(dir, cb) {
  let paths = dir.split('/');
  let index = 1;

  function next(index) {
    if (index > paths.length) return cb();
    let newPath = `${basePath}/views/${paths.slice(0, index).join('/')}`;
    fs.stat(newPath, function (err) {
      if (err) {
        fs.mkdir(newPath, function (err) {
          next(index + 1);
        });
      } else {
        next(index + 1);
      }
    })
  }
  next(index);
}

if (!dirName) {
  console.log(`示例：npm run tep ${capPirName}`);
  process.exit(0);
};
const capPirName = dirName.substring(0, 1).toUpperCase() + dirName.substring(1);

/**
 *  vue页面模版
 */
const VueTep = `<template>
  <div class="${dirName}-box">
    ${dirName}-Page
  </div>
</template>

<script lang="ts" src="./${dirName}.ts"></script>

<style lang="scss" scoped>
  @import './${dirName}.scss';
</style>

`

// ts 模版
const tsTep = `import { Component, Vue } from "vue-property-decorator";
import { Getter, Action, State } from 'vuex-class';
// import {  } from "@/components" // 组件

@Component({})
export default class ${moduleName} extends Vue {

  created() {
    //
  }
    
  activated() {
    //
  }

  mounted() {
    //
  }

  // 初始化函数
  init() {
    //
  }
    
}
`

// scss 模版
const lessTep = `.${dirName}-box {
  width: 100%;
}
`
mkdirSync(dirStr, () => {
  console.log('创建成功')
  process.chdir(`${basePath}/views/${dirStr}`) // cd views
  fs.writeFileSync(`${dirName}.vue`, VueTep) // vue 
  fs.writeFileSync(`${dirName}.ts`, tsTep) // ts
  fs.writeFileSync(`${dirName}.scss`, lessTep) // less

  process.exit(0)
});